package com.srwl.mytx.im.core;

import android.os.Handler;
import android.os.Looper;

import com.srwl.mytx.domain.Message;
import com.srwl.mytx.im.listener.MessageListener;
import com.srwl.mytx.im.protobuf.MessageProtobuf;

import java.util.List;


/**
 * <p>@ProjectName:     NettyChat</p>
 * <p>@ClassName:       MsgDispatcher.java</p>
 * <p>@PackageName:     com.freddy.im</p>
 * <b>
 * <p>@Description:     消息转发器，负责将接收到的消息转发到应用层</p>
 * </b>
 * <p>@author:          FreddyChen</p>
 * <p>@date:            2019/04/05 05:05</p>
 * <p>@email:           chenshichao@outlook.com</p>
 */
public class MsgDispatcher {

    private MessageListener mOnEventListener;
    private Handler handler = new Handler(Looper.getMainLooper());

    public MsgDispatcher() {

    }

    public void setOnEventListener(MessageListener listener) {
        this.mOnEventListener = listener;
    }

    public void receivedOffLineMsg(final List<Message> messages) {
        if (mOnEventListener == null) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                mOnEventListener.dispatchOffLineMsg(messages);
            }
        });

    }

    // TODO: 2019/6/25 如果登录失败 ，需要处理的话， 就应该在这儿把登录失败的消息 分发出去，应该写个登录失败的接口
    public void onLoginFail(MessageProtobuf.Msg loginFailMessage ){

    }
    /**
     * 接收消息，并通过OnEventListener转发消息到应用层
     *
     * @param msg
     */
    public void receivedMsg(final MessageProtobuf.Msg msg) {
        if (mOnEventListener == null) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                mOnEventListener.dispatchMsg(msg);
            }
        });

    }
}
